<?php
namespace app\controllers;

use app\common\corelib\CacheHelper;
use app\common\HttpCurl;
use app\common\Sms;
use app\logic\UserLogic;
use Yii;


class UserController extends BaseController
{

    /**
     * 个人中心首页
     * @apiDescription   by Jim
     * @api              {post} /user/index 个人中心首页
     * @apiName          index
     * @apiVersion       1.0.0
     * @apiParam {string} token 用户token
     * @apiGroup         User
     * @apiSampleRequest /user/index?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * @apiError         100000    获取失败
     */
    public function actionIndex(){

    }

    function generate_code($length = 6) {
        return rand(pow(10,($length-1)), pow(10,$length)-1);
    }

    /**
     * 发送短信
     * @apiDescription   by Jim
     * @api              {post} /user/get-code 发送短信
     * @apiName          get-code
     * @apiVersion       1.0.0
     * @apiParam {string} mobile 电话
     * @apiGroup         User
     * @apiSampleRequest /user/get-code?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * {
    }
     * @apiError         100001    获取失败
     */
    public function actionGetCode(){
        $mobile = $this->getQueryparams('mobile','');
        //$result = HttpCurl::zj_post('/gw/userAuth/get_smscode',['mobileNum'=>$mobile]);
        $send_info = new Sms();
        $code = $this->generate_code(6);
        $result = $send_info->sendInfo($mobile,'您的卡宝宝商城短信验证码为'.$code.'感谢您的支持。');
        if($result){
            CacheHelper::set($mobile,$code);
            $this->response();
        }else{
            $this->error('100001','fail');
        }
    }

    /**
     * 注册
     * @apiDescription   by Jim
     * @api              {post} /user/register 注册
     * @apiName          register
     * @apiVersion       1.0.0
     * @apiParam {string} mobile 电话
     * @apiParam {string} caphtaCode 验证码
     * @apiParam {string} password 密码
     * @apiGroup         User
     * @apiSampleRequest /user/register?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * {
    }
     * @apiError         40001    获取失败
     */
    public function actionRegister(){
        $mobile = $this->getQueryparams('mobile','');
        $caphtaCode = $this->getQueryparams('caphtaCode','');
        $password = $this->getQueryparams('password','');
        $result = HttpCurl::zj_post('/gw/userAuth/register',['login'=>['mobileNum'=>$mobile,'smsCode'=>$caphtaCode,'password'=>$password]]);
        if($result['errcode']==0){
            $userLogic = new UserLogic();
            $user = $userLogic->addUser($mobile,$result['token'],$password);
            $expireIn = $result['expireIn']!=-1?$result['expireIn']:0;
            CacheHelper::set('user_token_'.$result['token'],$user,$expireIn);
            CacheHelper::set('user'.$user['user_id'],$user,$expireIn);
            $this->response(['access_token'=>$result['token']]);
        }else{
            $this->error('40001',$result['errmsg']);
        }

    }

    /**
     * 登录
     * @apiDescription   by Jim
     * @api              {post} /user/login 登录
     * @apiName          login
     * @apiVersion       1.0.0
     * @apiParam {string} mobile 电话
     * @apiParam {string} code 验证码
     * @apiGroup         User
     * @apiSampleRequest /user/login?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * {
    }
     * @apiError         100000    获取失败
     */
    public function actionLogin(){
        $mobile = $this->getQueryparams('mobile','');
        $code = $this->getQueryparams('code','');

        //$result = HttpCurl::zj_post('/gw/userAuth/login',['login'=>['mobileNum'=>$mobile,'smsCode'=>$code]]);
        if($code==CacheHelper::get($mobile)){
            $userLogic = new UserLogic();
            $token = MD5($mobile . time());
            $user = $userLogic->updateUser($mobile,$token);
            CacheHelper::set('user_token_'.$token,$user);
            CacheHelper::set('user'.$user['user_id'],$user);
            $this->response(['access_token'=>$token]);
        }else{
            $this->error('100000','手机号或者验证码错误');
        }

    }

    /**
     * 修改密码
     * @apiDescription   by Jim
     * @api              {post} /user/update-password 修改密码
     * @apiName          update-password
     * @apiVersion       1.0.0
     * @apiParam {string} mobile 电话
     * @apiParam {string} caphtaCode 验证码
     * @apiParam {string} password 密码
     * @apiGroup         User
     * @apiSampleRequest /user/update-password?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * {
    }
     * @apiError         40003    获取失败
     */
    public function actionUpdatePassword(){
        $mobile = $this->getQueryparams('mobile','');
        $caphtaCode = $this->getQueryparams('caphtaCode','');
        $password = $this->getQueryparams('password','');
        $userLogic = new UserLogic();
        $time = time();
        $nonce = $this->GetRandStr(8);
        $appKey = \Yii::$app->params["zjapi"]["appKey"];
        $signature = array($appKey,$mobile,$caphtaCode,$password,$nonce,$time);
        sort($signature,2);
        $signature = implode('',$signature);
        $signature = md5($signature);
        $result = HttpCurl::zj_post('/gw/userAuth/resetPasswordWithSmscode',['login'=>['mobileNum'=>$mobile,'newpassword'=>$password,'smsCode'=>$caphtaCode,'timeStamp'=>$time,'nonce'=>$nonce,'signature'=>$signature]]);
        if($result['errcode']==0){
            $this->response();
        }else{
            $this->error('40003',$result['errmsg']);
        }

    }

    public function GetRandStr($length)
    {
        $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $len = strlen($str) - 1;
        $randstr = '';
        for ($i = 0; $i < $length; $i++) {
            $num = mt_rand(0, $len);
            $randstr .= $str[$num];
        }
        return $randstr;
    }

}